# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(tidyverse)
library(estimatr)
library(metafor)
library(broom)
library(magrittr)
library(ggbeeswarm)


ft_targets_ates_w1_PID_adj <- read_rds("fitted_models/ft_targets_ates_w1_PID_adj.rds")
ft_targets_meta_w1_PID_adj <- read_rds("fitted_models/ft_targets_meta_w1_PID_adj.rds")
ft_targets_ates_w1_all_adj <- read_rds("fitted_models/ft_targets_ates_w1_all_adj.rds")
ft_targets_meta_w1_all_adj <- read_rds("fitted_models/ft_targets_meta_w1_all_adj.rds")

# reassignment

t0 <- ft_targets_ates_w1_PID_adj
t1 <- ft_targets_meta_w1_PID_adj
t2 <- ft_targets_ates_w1_all_adj
t3 <- ft_targets_meta_w1_all_adj

t0 %<>% 
  bind_rows(
    t2 %>% 
      mutate(
        lucid_pid_3 = "Overall"
      )
  ) %>% 
  mutate(
    lucid_pid_3 = lucid_pid_3 %>% 
      factor(
        c(
          "Democrat",
          "Independent",
          "Republican",
          "Overall"
        )
      )
  )

t1 %<>% 
  bind_rows(
    t3 %>% 
      mutate(
        lucid_pid_3 = "Overall"
      )
  ) %>% 
  mutate(
    lucid_pid_3 = lucid_pid_3 %>% 
      factor(
        c(
          "Democrat",
          "Independent",
          "Republican",
          "Overall"
        )
      )
  )

t0$term %<>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effect",
    "Correction effect"
  ) %>% 
  factor(
    c("Misinformation effect",
      "Correction effect")
  )

t1$term %<>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effect",
    "Correction effect"
  ) %>% 
  factor(
    c("Misinformation effect",
      "Correction effect")
  )

t0$sig <- t0$p.value %>% 
  is_less_than(.05) %>% 
  ifelse(
    "p <.05", "p >= .05"
  )

# label significant effects

t4 <- t0 %>% 
  filter(
    p.value <= .05
  ) 

t4$lab <- t4$dv %>% 
  plyr::mapvalues(
    c("w1_ft_kamala_harris", "w1_ft_the_world_health_organization_(who)"),
    c("Kamala Harris", "WHO")
  )


t1$lab <- t1$estimate %>% 
  round(2) %>% 
  str_replace(fixed("0."), fixed("."))

t1$lab %<>% 
  str_detect("-") %>% 
  ifelse(
    t1$lab %>% 
      str_pad(width = 4, side = "right", pad = "0"),
    t1$lab %>% 
      str_pad(width = 3, side = "right", pad = "0")
  )

t1$lab %<>% 
  str_c(
    c("***", "**", "*", "") %>% 
      extract(
        t1$p.value %>% 
          findInterval(
            c(-Inf, .001, .01, .05, Inf)
          )
      )
  )


t0 %>% 
  ggplot() +
  geom_hline(
    yintercept = 0,
    linetype = "dashed"
  ) +
  geom_dotplot(
    aes(x = lucid_pid_3, y = estimate, 
        fill = sig
    ), 
    binwidth = .25, 
    # shape = 21,
    binaxis = "y",
    stackdir = "center",
    binpositions= "all", 
    # size = 5,
    dotsize = .8,
    # abs_size = T
  ) +
  geom_segment(
    aes(
      x = lucid_pid_3,
      xend = lucid_pid_3,
      y = conf.low,
      yend = conf.high
    ),
    size = 1,
    data = t1
  ) +
  geom_point(
    aes(
      x = lucid_pid_3,
      y = estimate
    ),
    data = t1,
    shape = 23,
    size = 9.5,
    fill = "white"
  ) +
  geom_text(
    aes(
      lucid_pid_3, estimate, label = lab
    ),
    size = 2.75,
    data = t1
  ) +
  geom_text(
    aes(
      y = estimate, 
      label = lab %>% str_replace(" ", "\n"),
      x = lucid_pid_3
    ),
    position = position_nudge(x = .075),
    data = t4,
    fontface = "italic",
    size = 2.5,
    lineheight = .7, 
    vjust = 0
  ) +
  coord_flip() +
  labs(
    x = "",
    y = "Difference on 100 point feeling thermometer"
  ) +
  facet_wrap(~term, nrow = 1) +
  scale_fill_grey(start = .3, end = 1)
